package com.mit.facade;

import java.util.List;

import org.apache.log4j.Logger;

import com.mit.common.RequestContext;
import com.mit.common.ResultContext;
import com.mit.common.exception.BaseException;
import com.mit.common.exception.DuplicateKeyException;
import com.mit.common.sale.dto.CustomerDTO;
import com.mit.common.security.dto.UserDTO;
import com.mit.common.token.SaleTokens;
import com.mit.common.token.Tokens;
import com.mit.service.core.dao.ISequenceDAO;
import com.mit.service.sale.dao.ICustomerDAO;

public class SaleFacade implements ISaleFacade {


	private Logger logger = Logger.getLogger(SaleFacade.class);
	
	private ISequenceDAO sequenceDAO;
	private ICustomerDAO customerDAO;
	


	public void setCustomerDAO(ICustomerDAO customerDAO) {
		this.customerDAO = customerDAO;
	}
	public void setSequenceDAO(ISequenceDAO sequenceDAO) {
		this.sequenceDAO = sequenceDAO;
	}
	@Override
	public ResultContext retrievePagedCustomers(RequestContext rqs) {
		UserDTO user = rqs.getUser();
		String key = (String) rqs.getParameter(SaleTokens.KEY_CUSTOMER_NAME);
		Integer pageNo = (Integer) rqs.getParameter(Tokens.KEY_PAGE_NO);
		Integer pageSize = (Integer) rqs.getParameter(Tokens.KEY_PAGE_SIZE);
		List<CustomerDTO> lst = customerDAO.retrievePagedCustomers(user.getOrgId(), key, pageNo, pageSize);
		ResultContext rlt = new ResultContext();
		rlt.addResult(SaleTokens.KEY_CUSTOMER_LIST, lst);
		return rlt;
	}

	@Override
	public ResultContext retrieveCustomerCount(RequestContext rqs) {
		UserDTO user = rqs.getUser();
		String key = (String) rqs.getParameter(SaleTokens.KEY_CUSTOMER_NAME);
		Integer rowCount = customerDAO.retrieveCustomersCount(user.getOrgId(),key);
		ResultContext rlt = new ResultContext();
		rlt.addResult(Tokens.KEY_RECORD_COUNT, rowCount);
		return rlt;
	}

	@Override
	public ResultContext saveCustomer(RequestContext rqs) throws BaseException {
		CustomerDTO customer = (CustomerDTO) rqs.getParameter(SaleTokens.KEY_CUSTOMER_DTO);
		UserDTO user = rqs.getUser();
		customer.setOrgId(user.getOrgId());
		
		if(customer.getId() < 1){
			logger.debug("new Customer");
			//验证：简称不能重复
			if(customerDAO.hasCustomer(user.getOrgId(), null, customer.getName())){
				logger.error("Duplicate Name: " + customer.getName());
				throw new DuplicateKeyException(customer.getName());
			}
			customer = customerDAO.createCustomerDTO(customer);
			logger.info("new Customer created: " + customer.getName());
		}else{
			logger.debug("update Customer");
			//验证：简称不能重复
			if(customerDAO.hasCustomer(user.getOrgId(),customer.getId(), customer.getName())){
				logger.error("Duplicate Name: " + customer.getName());
				throw new DuplicateKeyException(customer.getName());
			}
			customerDAO.updateCustomerDTO(customer);
			logger.info("Customer updated: " + customer.getName());
		}
		ResultContext rlt = new ResultContext();
		rlt.addResult(SaleTokens.KEY_CUSTOMER_DTO, customer);
		return rlt;
	}

	@Override
	public ResultContext retrieveCustomerBasicInfo(RequestContext rqs) {
		UserDTO user = rqs.getUser();
		String name = (String) rqs.getParameter(SaleTokens.KEY_CUSTOMER_NAME);
		List<CustomerDTO> lst = customerDAO.retrieveCustomerBasicInfo(user.getOrgId(), name);
		ResultContext rlt = new ResultContext();
		rlt.addResult(SaleTokens.KEY_CUSTOMER_LIST, lst);
		return rlt;
	}

}
